const SERVICE = require("../../lib/js/service.js");

Page({
  data: {
    navItem: 0,
    MyClaimList_page: 1,
    MyClaimList_all_page: null,
    maintenReports_page: 1,
    maintenReports_all_page: null,
    showNoData: false,
    loadingStatus: 0,   //上拉加载状态 0停止 1开启 2没有更多数据
    loadingStatus_2: 0,   //上拉加载状态 0停止 1开启 2没有更多数据    
    loadingVal: [0, "加载更多中...", "没有更多啦~"],
    record_2: [],
    record: []
  },

  onLoad(options) {
    let td = this.data;
    if(options.navItem){
      this.setData({
        navItem: options.navItem
      });
    }
    if (td.navItem == 0) {
      this.maintenReports();
    } else if (td.navItem == 1) {
      this.getMyClaimList();
    }

    if (options.showNavItem && options.showNavItem == 1){
      this.setData({
        navItem: options.showNavItem
      });
      this.getMyClaimList();
    }
  },

  /*分享*/
  onShareAppMessage() {

  },

  /*下拉刷新*/
  onPullDownRefresh() {
    let td = this.data;
    if (td.navItem == 0) {
      td.maintenReports_page = 1;
      this.maintenReports();
    } else if (td.navItem == 1) {
      td.MyClaimList_page = 1;
      this.getMyClaimList();
    }
    this.setData({
      record_2: [],
      record: [],

    });
  },

  /*上拉刷新*/
  onReachBottom() {
    let td = this.data;
    if (td.navItem == 0) {
      if (td.maintenReports_page + 1 <= td.maintenReports_all_page) {
        td.maintenReports_page++;
        this.maintenReports();
      } else if (td.maintenReports_page > 1 && td.maintenReports_page + 1 == td.maintenReports_all_page){
        this.setData({
          loadingStatus_2: 2
        });
      }

    } else if (td.navItem == 1) {
      if (td.MyClaimList_page + 1 <= td.MyClaimList_all_page) {
        td.MyClaimList_page++;
        this.getMyClaimList();
      }

    }
  },

  /*初始数据*/
  ActiveInitData() {
    this.setData({
      showInitLoading: false,
      myClaimListStatus: true,
    });
  },

  /*导航*/
  Nav(e) {
    let td = this.data, index = e.currentTarget.dataset.index;
    if (index == 1 && td.navItem != 1) {
      this.setData({
        navItem: index,
        record_2: [],
        MyClaimList_page: 1
      });
      this.getMyClaimList();

    } else if (index == 0 && td.navItem != 0) {
      this.setData({
        navItem: index,
        record: [],
        maintenReports_page: 1
      });
      this.maintenReports();
    }

  },

  /*初始化加载回调方法*/
  InitLoadingCellback() {
    let td = this.data;
    if (!td.myClaimListStatus) {
      if (td.navItem == 0) {
        this.maintenReports();
      } else if (td.navItem == 1) {
        this.getMyClaimList();
      }

    }
  },

  /*获取出险记录*/
  getMyClaimList(params) {
    let _this = this, td = this.data, arg;
    _this.ActiveInitData();

    arg = params ? params : params = {};
    // 默认参数
    params.thirdCookie = wx.getStorageSync("thirdCookie");
    params.page = td.MyClaimList_page;

    SERVICE.POST(SERVICE.URL.myClaimList, arg, (status, res) => {
      wx.stopPullDownRefresh();
      _this.setData({
        showInitLoading: true
      });
      console.log("获取出险记录:", res);
      if (res.data.status == "success") {
        _this.setData({
          showNoData: false,
          MyClaimList_all_page: res.data.all_page,
          record_2: td.record_2.concat(res.data.list)
        });

      } else {
        _this.setData({
          record_2: [],
          showNoData: true,
          myClaimListStatus: false
        });
      }
    });
  },

  /*获取维保记录*/
  maintenReports(params) {
    let _this = this, td = this.data, arg;
    _this.ActiveInitData();

    arg = params ? params : params = {};
    arg.thirdCookie = wx.getStorageSync("thirdCookie");
    arg.page = td.maintenReports_page;
    SERVICE.POST(SERVICE.URL.maintenReports, arg, (status, res) => {
      wx.stopPullDownRefresh();
      _this.setData({
        showInitLoading: true
      });
      console.log("获取维保记录:", res);
      if (res.data.status == "success") {
        _this.setData({
          showNoData: false,
          maintenReports_all_page: res.data.all_page,
          record: td.record.concat(res.data.list)
        });
      } else {
        _this.setData({
          record: [],
          showNoData: true,
          myClaimListStatus: false
        });
      }
    });
  },

  /*搜索*/
  SearchFormSubmit(e) {
    let td = this.data, form_obj = e.detail.value;
    td.MyClaimList_page = 1;
    td.maintenReports_page = 1;
    if (td.navItem == 0) {
      this.setData({
        record: []
      });
      this.maintenReports({
        query_vin: form_obj.search,

      });
    } else if (td.navItem == 1) {
      this.setData({
        record_2: []
      });
      this.getMyClaimList({
        query_vin: form_obj.search
      });
    }

  },

  /*键盘搜索按钮事件*/
  search(e) {
    let td = this.data, get_search = e.detail.value;
    if (td.navItem == 0) {
      this.setData({
        record: []
      });
      this.maintenReports({
        query_vin: get_search,
      });
    } else if (td.navItem == 1) {
      this.setData({
        record_2: []
      });
      this.getMyClaimList({
        query_vin: get_search,
      });
    }
  },

  getItem(e) {
    let item = e.currentTarget.dataset.item;
    if (item.orderStatus == "未支付") {
      this.createMainOrder({
        thirdCookie: wx.getStorageSync("thirdCookie"),
        CX: item.CX,
        vin: item.vin
      });
    } else if (item.orderStatus == "查询成功") {
      wx.navigateTo({
        url: "../web-view/web-view?orderId=" + item.orderId
      });
    }
  },

  getItem2(e){
    let item = e.currentTarget.dataset.item;
    if (item.orderStatus == "未支付") {
      this.createClaimOrder({
        thirdCookie: wx.getStorageSync("thirdCookie"),
        CX: item.CX,
        vin: item.vin
      });
    } else if (item.orderStatus == "查询成功") {
      wx.navigateTo({
        url: "../insurance-report/insurance-report?id=" + item.orderId
      });
      
    }
  },

  /*创建订单*/
  createMainOrder(params) {
    let _this = this, td = this.data;
    wx.showLoading({
      title: "创建订单中...",
      mask: true
    });
    SERVICE.POST(SERVICE.URL.createMainOrder, params, (stauts, res) => {
      wx.hideLoading();
      if (res.data.status == "success") {
        let pay_info = res.data;
        wx.navigateTo({
          url: "../order-pay/order-pay?pay_info=" + JSON.stringify(pay_info) + "&types=0",
        });

      } else {
        wx.showToast({
          title: "创建订单失败",
          icon: "none",
          mask: true
        });
      }

    });
  },

  /*创建订单*/
  createClaimOrder(params) {
    let _this = this, td = this.data;
    wx.showLoading({
      title: "创建订单中...",
      mask: true
    });
    SERVICE.POST(SERVICE.URL.createClaimOrder, params, (stauts, res) => {
      wx.hideLoading();
      if (res.data.status == "success") {
        let pay_info = res.data;
        wx.navigateTo({
          url: "../order-pay/order-pay?pay_info=" + JSON.stringify(pay_info) + "&types=1",
        });

      } else {
        wx.showToast({
          title: "创建订单失败",
          icon: "none",
          mask: true
        });
      }

    });
  },

  
});